home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
DEMO_VGA
/
FRSTM1.LZH
/
XBIT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-03-25
|
3KB
|
118 lines
/* Xbit.c by Mark C. Peterson, CompuServe [70441,3353] */
#include <string.h>
#include <malloc.h>
#include "xbit.h"
int
XBits, XBytes, IntXBits, DecXBits, ParDecXBits, ParDecXBytes,
MultShiftBits, MultBytes, XBitOverflow, XBitSign;
void SetXBit(int Bytes, int IntBits) {
XBytes = Bytes + 1;
XBytes >>= 1;
XBytes <<= 1;
XBits = Bytes << 3;
IntXBits = IntBits;
DecXBits = XBits - (IntBits + 1);
ParDecXBytes = DecXBits >> 3;
ParDecXBits = DecXBits & 7;
MultBytes = IntBits >> 3;
MultShiftBits = IntBits & 7;
}
int DoubleToXBit(double d, XBIT x) {
char Signed, dc[10];
int *di, Underflow;
/* 1. Setup integer alias */
di = (int*)dc;
/* 2. Clear 'x' */
strnset(x, 0, XBytes);
/* 3. Copy 'd' to char buffer */
memcpy(&dc[2], (char*)&d, 8);
di[0] = 0;
/* 4. Get and mask the sign bit */
Signed = dc[9] &0x80;
dc[9] &= 0x7f;
/* 5. Separate exponent from mantisa */
XBitShift(dc, 10, -4);
/* 6. Check for double = 0 (i.e. exponent = 0) */
if(!di[4])
return(0);
/* 7. Convert negative biased mantisa (ranged from 1.0 to 2.0)
and make room for sign bit */
XBitShift(dc, 8, -2);
dc[7] |= 0x40;
/* 8. Convert excess 1023 exponent to deficit 'IntXBits - 1' */
di[4] -= 1023 + (XBits - DecXBits - 2);
/* 9. Check for Overflow */
if(di[4] > 0)
return(di[4]);
/* 10. Check for Underflow */
Underflow = di[4] + XBits;
if(Underflow < 0)
return(Underflow);
/* 11. Move mantisa to 'x' */
if(XBytes >= 8)
memcpy(&x[XBytes - 8], dc, 8);
else
memcpy(x, &dc[8 - XBytes], XBytes);
/* 12. Right bit shift 'x' by exponent */
XBitShift(x, XBytes, di[4]);
/* 13. Negate 'x' if 'd' was signed */
if(Signed)
return(XBitNegate(x, XBytes));
else
return(0);
}
/* Not used by Malderbot draw program, but helps out in debugging */
double XBitToDouble(XBIT XBit) {
char *dc, *x;
double d = 0.0;
int Exponent, *di;
unsigned Sign;
dc = (char*)&d;
di = (int*)dc;
x = malloc(XBytes);
memcpy(x, XBit, XBytes);
if(x[XBytes - 1] < 0) {
XBitNegate(x, XBytes);
Sign = 0x8000;
}
else
Sign = 0;
for(Exponent = 0; Exponent < XBits; Exponent++) {
if(XBitShift(x, XBytes, 1)) {
if(XBytes >= 8) {
XBitShift(x, XBytes, -4);
memcpy(dc, &x[XBytes - 8], 7);
}
else {
memcpy(&dc[7 - XBytes], x, XBytes);
XBitShift(dc, 7, -4);
}
Exponent = 1023 + (IntXBits - Exponent);
di[3] |= (Exponent << 4) | Sign;
}
}
free(x);
return(d);
}